document.addEventListener('DOMContentLoaded', function() {
    const headerAndSearch = document.querySelector('.header-search-container');
    const placeholder = document.querySelector('.header-search-placeholder');

    // 获取初始位置和高度
    const originalTop = headerAndSearch.getBoundingClientRect().top + window.scrollY;
    const headerAndSearchHeight = headerAndSearch.offsetHeight;

    // 设置占位符高度
    placeholder.style.height = headerAndSearchHeight + 'px';

    function updateFixedState() {
        const scrollPosition = window.pageYOffset;

        if (scrollPosition >= originalTop) {
            if (!headerAndSearch.classList.contains('fixed')) {
                headerAndSearch.classList.add('fixed');
                placeholder.style.display = 'block';
            }
        } else {
            headerAndSearch.classList.remove('fixed');
            placeholder.style.display = 'none';
        }
    }

    // 初始调用一次以设置正确的初始状态
    updateFixedState();

    // 使用 requestAnimationFrame 以确保滚动事件更平滑
    window.addEventListener('scroll', function() {
        window.requestAnimationFrame(updateFixedState);
    });
});